<?php


namespace App\Services\Business\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Follow extends Model {
    use HasFactory;
    protected $table = 'followers';

    // 作者的关注者
    public function follower() {
        return $this->belongsToMany(User::class, 'followers', 'user_id', 'follower_id');
    }

    // 作者关注的人
    public function following() {
        return $this->belongsToMany(User::class, 'followers', 'follower_id', 'user_id');
    }

    // 判断作者是否已经被关注，此处$id为Auth::user()->id
    public function isFollower($id) {
        return $this->follower()->wherePivot('follower_id', $id)->first();
    }

    // 自动切换
    public function followerToggle($ids) {
        $ids = is_array($ids) ?: [$ids];
        return $this->follower()->withTimestamps()->toggle($ids);
    }



}
